package com.youyunsoft.commons.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class MyVeiw extends View {
    public MyVeiw(Context context) {
        super(context);
    }

    public MyVeiw(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        int r = getMeasuredWidth() / 2;//也可以是getMeasuredHeight()/2,本例中我们已经将宽高设置相等了
//        //圆心的横坐标为当前的View的左边起始位置+半径
//        int centerX = getLeft() + r;
//        //圆心的纵坐标为当前的View的顶部起始位置+半径
//        int centerY = getTop() + r;
//
//        Paint paint = new Paint();
//        paint.setColor(Color.GREEN);
//        //开始绘制
//        canvas.drawCircle(centerX, centerY, r, paint);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = getMySize(100, heightMeasureSpec);
        int width = getMySize(100, widthMeasureSpec);

        if (width < height) {
            height = width;
        } else {
            width = height;
        }

        /**
         * <p>This method must be called by {@link #onMeasure(int, int)} to store the
         * measured width and measured height. Failing to do so will trigger an
         * exception at measurement time.</p>
         * */
        setMeasuredDimension(width, height);

    }

    private int getMySize(int defaultSize, int measureSpec) {

        int mySize = defaultSize;

        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            case MeasureSpec.UNSPECIFIED://没有指定大小，就设置为默认大小
                mySize = defaultSize;

                break;

            case MeasureSpec.AT_MOST://如果测量模式是最大值为size，我们将大小取最大值，也可以取其他值
                mySize = size;

                break;

            case MeasureSpec.EXACTLY://如果是固定大小，就不要取改变他
                mySize = size;

                break;
            default:
                mySize = defaultSize;
        }
        return mySize;
    }

}
